package com.ljx.recruitment.user.service.impl;

import com.ljx.recruitment.security.util.UserUtil;
import com.ljx.recruitment.security.common.MenuConstant;
import com.ljx.recruitment.user.domain.entity.Menu;
import com.ljx.recruitment.user.domain.vo.resp.AdminOptionsResp;
import com.ljx.recruitment.user.domain.vo.resp.HomeOptionResp;
import com.ljx.recruitment.user.domain.vo.resp.UserOptionsResp;
import com.ljx.recruitment.user.service.OptionService;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-11-01  21:27
 */
@Service
public class OptionServiceImpl implements OptionService {

    @Override
    public AdminOptionsResp getAdminOpts() {
        Map<String, Menu> menuMap = UserUtil.getUserInfo().getMenus()
                .stream()
                .collect(Collectors.toMap(Menu::getCode, Function.identity()));
        AdminOptionsResp adminOptionsResp = new AdminOptionsResp();
        adminOptionsResp.setOpDashboardAble(menuMap.containsKey(MenuConstant.DASHBOARD_ADMIN));
        adminOptionsResp.setOpThemeAble(menuMap.containsKey(MenuConstant.THEME_ADMIN));
        adminOptionsResp.setOpUserAble(menuMap.containsKey(MenuConstant.USER_ADMIN));
        adminOptionsResp.setOpPermissionAble(menuMap.containsKey(MenuConstant.PERMISSION_ADMIN));
        adminOptionsResp.setOpTenantAble(menuMap.containsKey(MenuConstant.TENANT_ADMIN));
        adminOptionsResp.setOpJobAble(menuMap.containsKey(MenuConstant.JOB_ADMIN));
        adminOptionsResp.setOpInterviewAble(menuMap.containsKey(MenuConstant.INTERVIEW_ADMIN));
        adminOptionsResp.setOpResumeItemConfigAble(menuMap.containsKey(MenuConstant.RESUME_CONFIG));
        adminOptionsResp.setOpResumeItemAdminAble(menuMap.containsKey(MenuConstant.RESUME_CONFIG_ADMIN));
        adminOptionsResp.setOpTrailApplicationAble(menuMap.containsKey(MenuConstant.TRIAL_APPLICATION_ADMIN));

        return adminOptionsResp;
    }

    @Override
    public UserOptionsResp getUserOpts() {
        Boolean isStaff = UserUtil.checkIsStaff();
        Boolean isUser = UserUtil.checkIsUser();
        UserOptionsResp resp = new UserOptionsResp();
        resp.setOpResumeAble(isUser);
        resp.setOpDeliverAble(isUser);
        resp.setOpRecommendAble(isStaff);
        return resp;
    }

    @Override
    public HomeOptionResp getHomeOpts() {
        HomeOptionResp resp = new HomeOptionResp();
        Boolean opAdminAble = Objects.nonNull(UserUtil.getUserInfo()) && !UserUtil.getUserInfo().getMenus().isEmpty();
        resp.setOpAdminAble(opAdminAble);

        return resp;
    }
}
